const {ccclass, property} = cc._decorator;

@ccclass
export default class loadingAni extends cc.Component {

    @property({displayName:"是否自动循环播"})
    isAuto:boolean = false;

    @property({displayName:"动画速度"})
    aniSpeed:number = 1;

    @property({type:cc.Node,displayName:"动画节点"})
    aniNode:cc.Node = null;

    @property({type:cc.Node,displayName:"进度节点"})
    progress:cc.Node = null;

    onLoad(){
        this.fontAni();
        // if(this.isAuto){
        //     this.autoAni();
        // }
    }

    async fontAni(){
        this.initLoading();
        return new Promise<void>(resolve=>{
            this.aniNode.children.forEach(async (n:cc.Node,index:number)=>{
                await this.setTimeOut(0.3*index);
                n.opacity = 0;
                n.scale = 0;
                cc.tween(n).to(0.5,{opacity:255,scale:1},{easing:cc.easing.backOut}).delay(0.2).call(()=>{
                    if(index == this.aniNode.childrenCount-1){
                        this.initAniNode();
                        this.fontAni();
                        resolve();
                    }
                }).start();
            });
        });
    }

    initAniNode(){
        this.aniNode.children.forEach((element:cc.Node)=>{
            element.opacity = 0;
        });
    }

    initLoading(){
        // this.progress.fillRange = 0;
        this.progress.children.forEach((n:cc.Node)=>{
            n.width = 0;
        });
        this.initAniNode();
    }

    setProgress(progress:number){
        // cc.tween(this.progress).to(0.2,{fillRange:progress}).start();
        let p = progress;
        let pCopy = progress;
        let progressArr:number[] = [0.125,0.375,0.125,0.375];
        let progressWidthArr:number[] = [199,530,206,530];
        let index:number = 0;
        for(let i =0;i<progressArr.length;i++){
            p -= progressArr[i];
            if(p<=0){
                index = i;
                break;
            }
        }
        this.progress.children.forEach((n:cc.Node,nIndex:number)=>{
            if(nIndex<index){
                n.width = progressWidthArr[nIndex];
            }else if(nIndex == index){
                let needWidth = progressWidthArr[nIndex];
                let needProgress = progressArr[nIndex];
                let nowProgress = Math.abs(p);
                let nowWidth = (1 - nowProgress/needProgress) * needWidth;
                n.width = nowWidth;
            }else{
                n.width = 0;
            }
        });
    }

    // autoAni(){
    //     // let tween = cc.tween(this.progress).to(4,{fillRange:-1}).call(()=>{
    //     //     this.progress.fillRange = 0;
    //     // });
    //     // cc.tween(this.progress).repeatForever(tween).start();

    //     this.schedule(()=>{
    //         this.setProgress()
    //     })

    // }

    autoprogress:number = 0;

    protected update(dt: number): void {
        if(this.isAuto){
            this.autoprogress += dt;
            if(this.autoprogress>=1){
                this.autoprogress = 0;
            }
            this.setProgress(this.autoprogress);
        }
    }

    setTimeOut(time:number){
        return new Promise<void>(resolve=>{
            cc.tween(this.node).delay(time).call(resolve).start();
        })
    }
    
}
